package com.cpj.bsyyzx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpj.bsyyzx.entity.FileNum;
import com.cpj.bsyyzx.entity.HealthFile;
import com.cpj.bsyyzx.entity.KfRecord;
import com.cpj.bsyyzx.mapper.FileNumMapper;
import com.cpj.bsyyzx.service.IFileNumService;
import com.cpj.bsyyzx.service.IHealthFileService;
import com.cpj.bsyyzx.service.IKfRecordService;
import com.cpj.bsyyzx.util.CommonResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 五个臭皮匠
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/kfRecord")
public class KfRecordController {
    @Resource
    IFileNumService fileNumService;

    @Resource
    FileNumMapper fileNumMapper;
        @Resource
        IKfRecordService kfRecordService;

        //    @RequestMapping("/list")
//    @ResponseBody
//    CommonResult pagelist(Page page){
//        QueryWrapper<HealthFile> queryWrapper = new QueryWrapper<>();
////        log.debug("分页查询");
//        IPage<HealthFile> rpage=healthFileService.selectPageVo(page,queryWrapper);
//        return CommonResult.success(rpage);
//    }
        @GetMapping("/list")
        @ResponseBody
        CommonResult page(Page page, KfRecord kfRecord) {

            QueryWrapper queryWrapper = new QueryWrapper();
            if (kfRecord != null && StringUtils.isNotEmpty(kfRecord.getUserName())) {
                queryWrapper.like("user_name", kfRecord.getUserName());
            }
            return CommonResult.success(kfRecordService.page(page, queryWrapper));
        }


        @RequestMapping("/update")
        @ResponseBody
        CommonResult update(KfRecord healthFile) {
            return CommonResult.success(kfRecordService.updateById(healthFile));
        }

        @RequestMapping("/add")
        @ResponseBody
        CommonResult add(KfRecord kfRecord) {
            FileNum fileNum = fileNumMapper.selectFileByKf();
            Integer list = fileNum.getMaxnum();
            list+=1;
            fileNum.setMaxnum(list);
            fileNumService.saveOrUpdate(fileNum);
            kfRecord.setRecordNum("Kfrecod"+list);
            if (kfRecordService.add(kfRecord))
                return CommonResult.success("ok");
            else
                return CommonResult.failed("姓名已经存在");
        }

        @RequestMapping("toedit")
        @ResponseBody
        CommonResult toedit(Integer reId) {
            return CommonResult.success(kfRecordService.getById(reId));
        }

        @RequestMapping("delete/{reId}")
        @ResponseBody
        CommonResult delete(@PathVariable Integer reId) {
//        log.debug("bbb根据主键删除:" + id);
            boolean success = kfRecordService.removeById(reId);
            return CommonResult.success(success);
        }

        @RequestMapping("/batchdel")
        @ResponseBody
        CommonResult batchdel(Integer[] id) {
            return CommonResult.success(kfRecordService.removeByIds(Arrays.asList(id)));
        }
}